package cn.kgc.javaweb.servlet;

import cn.kgc.javaweb.entity.Role;
import cn.kgc.javaweb.service.RoleService;
import cn.kgc.javaweb.service.impl.RoleServiceImpl;
import cn.kgc.javaweb.utils.JSONResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/5/23
 * @Description:
 */
@WebServlet("/role")
public class RoleServlet  extends BaseServlet {

    private RoleService roleService = new RoleServiceImpl();
    // 获取角色列表
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Role>  roles = roleService.getList();

        JSONResult.writeJson(roles,resp);

    }


    // 角色分页查询
    protected void rolePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取分页数据
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        HashMap<String,Object> map  =  roleService.selectPage(page,limit);
        JSONResult.writeJson(map,resp);

    }


    // 添加角色信息  addRole
    protected void addRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取分页数据
        String roleName = req.getParameter("roleName");

        HashMap<String,Object> map  =  roleService.addRole(roleName);
        JSONResult.writeJson(map,resp);

    }

    //根据id查询角色信息
    protected void getById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取id
         Integer id = Integer.valueOf(req.getParameter("id")) ;
         Role role =  roleService.getById(id);
        JSONResult.writeJson(role,resp);

    }

    // 根据id 更新角色信息
    protected void updateRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取分页数据
        String roleName = req.getParameter("roleName");
        Integer id = Integer.valueOf(req.getParameter("id")) ;

        HashMap<String,Object> map  =  roleService.updateRole(roleName,id);
        JSONResult.writeJson(map,resp);

    }

    // 根据id 删除角色信息    delete
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer id = Integer.valueOf(req.getParameter("id")) ;
        System.out.println("id = " + id);
        HashMap<String,Object> map  =  roleService.deleteById(id);
        JSONResult.writeJson(map,resp);

    }



}
